package com.xiguaji.boot.data.clickhouse;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.yandex.clickhouse.ClickHouseDataSource;

import javax.sql.DataSource;

@Configuration
@ConditionalOnClass(ClickHouseDataSource.class)
public class ClickHouseConfiguration {
    @Bean("clickhouseTemplate")
    public ClickHouseTemplate clickhouseTemplate(@Qualifier("clickhouseDataSource") DataSource dataSource) {
        return new ClickHouseTemplate(dataSource);
    }

    @Bean("clickhouseDataSource")
    @ConfigurationProperties(prefix = "spring.clickhouse")
    public DataSource clickhouseDataSource() {
        return DataSourceBuilder.create().build();
    }
}
